<?php
/**
 * @Author: Wang chunsheng  email:2192138785@qq.com
 * @Date:   2020-05-25 16:53:06
 * @Last Modified by:   Wang chunsheng  email:2192138785@qq.com
 * @Last Modified time: 2020-05-25 16:53:41
 */
use common\addons\diandi_shop\models\enums\OrderStatus;
use yii\grid\GridView;
use richardfan\widget\JSRegister;
use yii\helpers\Html;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $searchModel app\modules\diandi_shop\models\searchs\DdOrderSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Dd Orders';

$this->params['breadcrumbs'][] = $this->title;

$status = Yii::$app->request->queryParams;
$order_status = $status['DdOrderSearch']['order_status'];
?>


<ul class="nav nav-tabs">
    <li class="<?= isset($order_status) ? '' : 'active'; ?>">
        <?= Html::a('全部', ['index'], ['class' => '']); ?>
    </li>
    <?php foreach (OrderStatus::listData() as $key => $item) : ?>
        <li class="<?= $order_status == $key && $order_status != '' ? 'active' : ''; ?>">
            <?= Html::a($item, Url::to(['index', 'DdOrderSearch[order_status]' => $key]), ['class' => '']); ?>
        </li>
    <?php endforeach; ?>



</ul>

<div class="firetech-main" style="margin-top:20px;">
    <object id="LODOP_OB" classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" width=0 height=0>
        <embed id="LODOP_EM" type="application/x-print-lodop" width=0 height=0></embed>
    </object>
    <div class="dd-order-index ">
        
        <div class="panel panel-info">
              <div class="panel-heading">
                    <h3 class="panel-title">订单检索</h3>
              </div>
              <div class="panel-body">
              <?php  echo $this->render('_search', ['model' => $searchModel]); ?>

              </div>
        </div>
        
        
        <div class="panel panel-default">
              <div class="panel-heading">
                    <h3 class="panel-title">订单列表</h3>
              </div>
              <div class="panel-body">
        <?=
            GridView::widget([
                'dataProvider' => $dataProvider,
                'layout' => "{items}\n{pager}",
                'id' => 'ordergrid',
                'headerRowOptions' => ['class' => 'kartik-sheet-style'],
                'filterRowOptions' => ['class' => 'kartik-sheet-style'],
                'showFooter' => true,  //设置显示最下面的footer

                'columns' => [
                    // 'order_id',
                    [
                        'class' => 'yii\grid\CheckboxColumn',
                        'name' => 'id',
                        'headerOptions' => ['width' => '30'],
                        'footer' => '<button href="#" class="btn  btn-primary"  id="deletes">批量删除</button>',
                        // 'footerOptions' => ['colspan' => 2],
                    ],
                    [
                        'label' => '订单编号',
                        'attribute' => 'order_no',
                        'footer' => '<button  class="btn  btn-primary"  id="ExportData">批量导出</button>',
                    ],
                    [
                        'label' => '订单状态',
                        'attribute' => 'order_status',
                        'filter' => Html::dropDownList('DdOrderSearch[order_status]', $searchModel->order_status, OrderStatus::listData(), [
                            'prompt' => ['text' => '全部', 'options' => ['value' => '']],
                            'class' => 'form-control',
                        ]),
                        'value' => function ($date) {
                            return OrderStatus::getLabel($date->order_status);
                        },
                        'headerOptions' => ['style' => 'width:120px;'],
                        'footer' => $this->render('prints', []),
                    ],
                    [
                        'label' => '下单时间',
                        'attribute' => 'create_time',
                        'filter' => \yii\jui\DatePicker::widget([
                            'name' => 'DdOrderSearch[create_time]',
                            'options' => [
                                'id' => 'create_time_id',
                                'class' => 'form-control',
                            ],
                        ]),
                        'value' => function ($date) {
                            return date('Y-m-d H:i:s', $date->create_time);
                        },
                        'footer' => '<a href="javascript:CheckIsInstall()" class="btn  btn-primary">查看本机是否安装(控件或web打印服务)</a>',

                        // 'headerOptions' => ['style' => 'width:120px']
                    ],
                    // 'create_time:datetime',
                    'total_price',
                    'pay_price',
                    'pay_status',
                    //'pay_time:datetime',
                    // 'express_price',
                    //'express_company',
                    //'express_no',
                    //'delivery_status',
                    //'delivery_time:datetime',
                    //'receipt_status',
                    //'receipt_time:datetime',
                    //'transaction_id',
                    //'user_id',
                    //'wxapp_id',
                    //'update_time:datetime',
                    // [
                    //     'class' => EditableColumn::class,
                    //     'attribute' => 'order_status',
                    //     'url' => ['change-username'],
                    //     'type' => 'select',
                    //     'editableOptions' => function ($model) {
                    //         return [
                    //             'source' => OrderStatus::listData(),
                    //             'value' => $model->order_status,
                    //         ];
                    //     },
                    // ],
                    // [
                    //     'class' => EditableColumn::class,
                    //     'attribute' => 'total_price',
                    //     'url' => ['change-username'],
                    // ],
                    [
                        'class' => 'common\components\ActionColumn',
                        'header' => '操作',
                        'template' => '{view}',
                        'buttons' => [
                            'view' => function ($url, $model, $key) {
                                return  Html::a('<button type="button" class="btn btn-block btn-primary btn-sm">订单详情</button>', $url, ['title' => '订单详情']);
                            },
                        ],
                        // 'buttons' => [],
                        'headerOptions' => ['width' => '100'],
                    ],
                ],
            ]); ?>
                 </div>
        </div>
        
    </div>
</div>


<!-- 打印预览 -->
<?php JSRegister::begin([
    'id' => 'orders',
]);
?>

<?php $this->registerJsFile('@web/resource/js/LodopFuncs.js', ['depends' => ['yii\bootstrap\BootstrapAsset']]); ?>

<?php $this->registerJsFile('@web/resource/js/vue/vue.main.js', ['depends' => ['yii\bootstrap\BootstrapAsset']]); ?>


<script>
    $('#deletes').click(function(event) {
        console.log(event)
        var ids_v = $("#ordergrid").yiiGridView("getSelectedRows");
        console.log(ids_v)
        $.ajax({
            type: 'POST',
            url: "<?= Url::toRoute(['order/dd-order/deletes']); ?>",
            data: {
                'ids': ids_v
            },
            dataType: 'JSON',
            success: function(res) {
                if (res.code == 200) {
                    alert('删除成功')
                    window.location.reload();
                }
                console.log(res)
            },
        });
    })

    $('#ExportData').click(function(event) {
        var ids_v = $("#ordergrid").yiiGridView("getSelectedRows");

        window.open("<?= Url::toRoute(['order/dd-order/exportdata']); ?>?ids="+ids_v)
    })

 

    if ($.needCLodop()) {
        $.loadCLodop('<?= Yii::$app->settings->get('DiandiShopStroe', 'Lodop_ip'); ?>');
    } //加载

    var vm = new Vue({
        el: '#prints-content',
        data: {
            title: "打印标题",
            order:{},
            store:{},
            date: ''
        },
        methods: {
            details: function() {
                return  this.site + " - 学的不仅是技术，更是梦想！";
            }
        }
    });   
    
    $('.prints-modal').click(function(event) {
        // let vmm = vm; 
        console.log(event)
        var ids_v = $("#ordergrid").yiiGridView("getSelectedRows");
        $.post("<?= Url::to(['prints']); ?>", {ids:ids_v},
            function (res, textStatus, jqXHR) {
                console.log(res,vm,res.data.store.wxappName)
                vm.$data.title = res.data.store.wxappName;
                vm.$data.store = res.data.store;
                vm.$data.order = res.data.order;
                vm.$data.date = res.data.time;
            },
            "json"
        );
    })
 
</script>
<?php JSRegister::end(); ?>

<script>

    var LODOP; //声明为全局变量 
    function prn1_preview() {
        CreateOneFormPage();
        LODOP.PREVIEW();
    };

    function prn1_print() {
        CreateOneFormPage();
        LODOP.PRINT();
    };

    function prn1_printA() {
        CreateOneFormPage();
        LODOP.PRINTA();
    };

    function CreateOneFormPage() {
        LODOP = $.getLodop();
        LODOP.PRINT_INIT("打印控件功能演示_Lodop功能_表单一");
        LODOP.SET_PRINT_STYLE("FontSize", 18);
        LODOP.SET_PRINT_STYLE("Bold", 1);
        LODOP.ADD_PRINT_HTM(10, 10, 350, 600, document.getElementById("prints-content").innerHTML);
    };

    function prn2_preview() {
        CreateTwoFormPage();
        LODOP.PREVIEW();
    };

    function prn2_manage() {
        CreateTwoFormPage();
        LODOP.PRINT_SETUP();
    };

    function CheckIsInstall() {

        try {
            var LODOP = $.getLodop();
            if (LODOP.VERSION) {
                if (LODOP.CVERSION)
                    alert("当前有WEB打印服务C-Lodop可用!\n C-Lodop版本:" + LODOP.CVERSION + "(内含Lodop" + LODOP.VERSION + ")");
                else
                    alert("本机已成功安装了Lodop控件！\n 版本号:" + LODOP.VERSION);

            };
        } catch (err) {
            alert("本机已成功安装了Lodop控件！\n 版本号:" + LODOP.VERSION);

        }
    };
</script>